import {
	MessageConstant
} from "zpub_http/constant/MessageConstant.js"
import {
	WhatConstant
} from "zpub_http/constant/WhatConstant.js"
import {
	HttpParamKeyValue
} from "zpub_svr/constant/HttpParamKeyValue.js"
import {
	HttpProtocol
} from "zpub_http/http/HttpProtocol.js"
import {
	BaseService
} from "zpub_http/service/BaseService.js"
import {
	MajorMinorBusiness
} from "../../constant/MajorMinorBusiness.js"
import {
	FldValueBusiness
} from "../../constant/FldValueBusiness.js"
import {
	MajorMinorConstant
} from "zpub_db/constant/MajorMinorConstant.js"
import {
	JsonUtil
} from 'zpub_bas/util/JsonUtil.js'
import {
	UserMethod
} from "zpub_db/method/UserMethod.js"
/*
 * 功能：公共服务模块
 * 需要传入的键：
 * 传入的值类型：
 * 传入的值含义：
 * 是否必传 ：
 * 作者：郑朝军 on 2019/4/7 23:23
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 */
class ShopService extends BaseService {

	/*
	 * 查询商品<br/>
	 */
	static queryShop(page, listener) {
		listener.showLoading({
			title: '正在查询商品'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubQueryControl')
			.setMethod('queryExpLimit')
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, MajorMinorBusiness.MAJOR_BUSINESS)
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, MajorMinorBusiness.MINOR_SHOP_SHOP)
			.addParam('exp', "type = 1 or type = 2  or type = 3 or type = 4 or type = 5")
			.addParam('limit', page + "," + 15)
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("queryShop", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				// [shopGroup=团购]
				// [categoryList=分类精选]
				// [bgList=背景精选择]
				// [bgList2=背景精选择]
				// [loveList=猜你喜欢]
				let map = {
					shopGroup: [],
					categoryList: [],
					bgList: [],
					bgList2: [],
					loveList: [],
				};
				data.result.list.forEach((item, index, array) => {
					if (item.type == 1) {
						map.shopGroup.push(item);
					} else if (item.type == 2) {
						map.categoryList.push(item);
					} else if (item.type == 3) {
						map.bgList.push(item);
					} else if (item.type == 4) {
						map.bgList2.push(item);
					} else if (item.type == 5) {
						map.loveList.push(item);
					}
				});

				this.sendMessage("queryShop", map, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("queryShop", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}


	/*
	 * 查询商品附件<br/>
	 */
	static queryMedia(shopID, listener) {
		listener.showLoading({
			title: '正在查询附件'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubQueryControl')
			.setMethod('queryForFieldValuesExp')
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, MajorMinorConstant.SYS_NO_99)
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, MajorMinorBusiness.MINOR_MEDIA)
			.addParam('exp', " major = " + MajorMinorBusiness.MAJOR_BUSINESS + " AND minor = " + MajorMinorBusiness.MINOR_SHOP_SHOP +
				" and entityid=" + shopID)
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("queryMedia", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				let map = {
					banner: [],
					desc: '',
				};
				let desc = [];
				for (let item of data.result) {
					if (item.bustype == FldValueBusiness.MEDIA_BUSTYPE) {
						map.banner.push(item);
					} else {
						desc.push(`<img style="width:100%;display:block;" src="${item.filename}" />`);
					}
				}

				map.desc = `<div style="width:100%">
								${desc.toString()}
							</div>`;
				this.sendMessage("queryMedia", map, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("queryMedia", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}

	/*
	 * 创建商品订单<br/>
	 */
	static createOrder(json, listener) {
		listener.showLoading({
			title: '正在提交订单'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubCreateControl')
			.setMethod('create')
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, MajorMinorBusiness.MAJOR_BUSINESS)
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, MajorMinorBusiness.MINOR_SHOP_ORDER)
			.addParam('json', JsonUtil.jsonToString(json))
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("createOrder", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				this.sendMessage("createOrder", data.result, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("createOrder", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}

	/*
	 * 查询秒杀商品<br/>
	 */
	static querySeckill(listener) {
		listener.showLoading({
			title: '正在查询秒杀商品'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubQueryControl')
			.setMethod('queryForAll')
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, MajorMinorBusiness.MAJOR_BUSINESS)
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, MajorMinorBusiness.MINOR_SHOP_SECKILL)
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("querySeckill", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				this.sendMessage("querySeckill", data.result, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("querySeckill", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}

	/*
	 * 查询banner图片<br/>
	 */
	static queryBanner(listener) {
		listener.showLoading({
			title: '正在查询'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubQueryControl')
			.setMethod('queryForFieldValuesExp')
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, MajorMinorConstant.SYS_NO_99)
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, MajorMinorBusiness.MINOR_MEDIA)
			.addParam('exp', " major = 0 AND minor = 0")
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("queryBanner", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				this.sendMessage("queryBanner", data.result, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("queryBanner", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}

	/*
	 * 查询购物车<br/>
	 */
	static queryShopCart(listener) {
		listener.showLoading({
			title: '正在查询购物车'
		});
		let json = [{
			"_major": "97",
			"_minor": "7",
			"as": "a",
			"cols": "nums,time,sizecolor",
			"types": "i"
		}, {
			"_major": "97",
			"_minor": "2",
			"as": "b",
			"cols": "*",
			"types": "i"
		}]
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubQueryMultiTableControl')
			.setMethod('queryMultiTableUnion')
			// .addParam('exp', " a.userid =" + UserMethod.getUser().name + " AND state = " + 0)
			.addParam('exp', "a.shopid = b.id AND a.userid =1 AND a.state = " + 0)
			.addParam('json', JsonUtil.jsonToString(json))
			.addParam('pageno', 1)
			.addParam('pagesize', 15)
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("queryShopCart", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				this.sendMessage("queryShopCart", data.result.list, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("queryShopCart", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}

	/*
	 * 删除购物车<br/>
	 */
	static deleteShopCart(listener) {
		listener.showLoading({
			title: '正在删除'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubDeleteControl')
			.setMethod('deleteForEq')
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, MajorMinorBusiness.MAJOR_BUSINESS)
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, MajorMinorBusiness.MINOR_SHOP_CART)
			.addParam('fieldName', "userid")
			// .addParam('value', UserMethod.getUser().id)
			.addParam('value', '1')
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("deleteShopCart", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				this.sendMessage("deleteShopCart", data.result, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("deleteShopCart", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}

	/*
	 * 删除一个商品<br/>
	 */
	static deleteShopCartItem(index,shopid, listener) {
		listener.showLoading({
			title: '正在删除'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubDeleteControl')
			.setMethod('deleteForEq')
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, MajorMinorBusiness.MAJOR_BUSINESS)
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, MajorMinorBusiness.MINOR_SHOP_CART)
			.addParam('fieldName', "shopid")
			.addParam('value', shopid)
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("deleteShopCartItem", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				data.index = index;
				this.sendMessage("deleteShopCartItem", data, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("deleteShopCartItem", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}
	
	/*
	 * 查询商品浏览历史<br/>
	 */
	static queryShopHistory(listener) {
		listener.showLoading({
			title: '正在查询购物车'
		});
		let json = [{
			"_major": "97",
			"_minor": "8",
			"as": "a",
			"cols": "time",
			"types": "i"
		}, {
			"_major": "97",
			"_minor": "2",
			"as": "b",
			"cols": "*",
			"types": "i"
		}]
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubQueryMultiTableControl')
			.setMethod('queryMultiTableUnion')
			// .addParam('exp', " a.userid =" + UserMethod.getUser().name + " AND state = " + 0)
			.addParam('exp', "a.shopid = b.id AND a.userid =1")
			.addParam('json', JsonUtil.jsonToString(json))
			.addParam('pageno', 1)
			.addParam('pagesize', 15)
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("queryShopHistory", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				this.sendMessage("queryShopHistory", data.result.list, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("queryShopHistory", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}
}

export {
	ShopService
}
